package com.lzh.prometheus.config.exceptionnotice;

import com.lzh.prometheus.config.annos.ConditionalOnExceptionNotice;
import com.lzh.prometheus.httpclient.DingdingHttpClient;
import com.lzh.prometheus.message.DingDingNoticeSendComponent;
import com.lzh.prometheus.message.INoticeSendComponent;
import com.lzh.prometheus.pojos.ExceptionNotice;
import com.lzh.prometheus.properties.enums.DingdingTextTypeEnum;
import com.lzh.prometheus.properties.notice.DingDingNoticeProperty;
import com.lzh.prometheus.text.ExceptionNoticeResolver;
import com.lzh.prometheus.text.markdown.ExceptionNoticeMarkdownMessageResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhehen.lu
 * @date 2025/8/15 9:56
 */
@Configuration
@ConditionalOnExceptionNotice
@ConditionalOnProperty(value = "prometheus.dingding.enabled", havingValue = "true")
public class ExceptionNoticeDingdingSendingConfig {
    private final Log logger = LogFactory.getLog(ExceptionNoticeDingdingSendingConfig.class);

    @Bean("dingdingSendingComponent")
    @ConditionalOnMissingBean(name = "dingdingSendingComponent")
    public INoticeSendComponent<ExceptionNotice> sendComponent(DingdingHttpClient dingdingHttpClient,
                                                               ExceptionNoticeResolver exceptionNoticeResolver, DingDingNoticeProperty dingDingNoticeProperty) {
        logger.debug("注册钉钉通知");
        INoticeSendComponent<ExceptionNotice> component = new DingDingNoticeSendComponent<ExceptionNotice>(
                dingdingHttpClient, exceptionNoticeResolver, dingDingNoticeProperty);
        return component;
    }

    @Bean
    @ConditionalOnMissingBean
    public ExceptionNoticeResolver ExceptionNoticeTextResolver(DingDingNoticeProperty dingDingNoticeProperty) {
        if (dingDingNoticeProperty.getDingdingTextType() == DingdingTextTypeEnum.MARKDOWN)
            return new ExceptionNoticeMarkdownMessageResolver();
        return x -> x.createText();
    }
}
